
//
//  ZStoreViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/4/23.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZStoreViewController.h"
#import "ZStoreSubViewController.h"
#import "ZJScrollPageView.h"
#import "SDCycleScrollView.h"
#import "BannerModel.h"
#import "StoreModel.h"
@interface ZStoreViewController ()<ZJScrollPageViewDelegate,SDCycleScrollViewDelegate>

@property (nonatomic,strong)SDCycleScrollView *cycleView;

@property(nonatomic,assign)NSInteger currentIndex;
@property (nonatomic,strong)NSMutableArray *bannerArray;
@property (nonatomic,strong)NSMutableArray *titleArray;
@property (nonatomic,strong)NSMutableArray *dataArray;
/**
 页面分栏的标题
 */
@property (strong,nonatomic) NSArray<NSString *>*titles;

/**
 横向分页滑动组件
 */
@property (strong,nonatomic) ZJScrollPageView *scrollPageView;

@end

@implementation ZStoreViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
     [self.navigationItem setItemWithTitle:@"商城" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    self.dataArray = [NSMutableArray array];
    self.bannerArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    
    ///轮播图数据
    [self loadBanner];
    ///商城分类标题列表
    [self loadStoreCategoryTitle];
    
    [self addBannerView];
    
    // Do any additional setup after loading the view from its nib.
}
- (void)addTabTitlesWithTitles:(NSArray *)titles{
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.scrollLineColor = NColorFrom16RGB(0x23b7e5);
    style.selectedTitleColor = NColorFrom16RGB(0x23b7e5);
    style.segmentViewBounces = YES;
    style.titleFont = kFont15;
    style.adjustCoverOrLineWidth = YES;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 45;
    self.titles = titles;
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, cycleHight, ScreenWidth, ScreenHeight - 20.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
    
}
#pragma mark ZJPhotoScrollViewDelegate
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
//    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
//    
//    if (!childVc) {
//        
//        childVc = [[ZStoreSubViewController alloc] init];
//        
//    }
    ZStoreSubViewController *childVc = (ZStoreSubViewController *)reuseViewController;
    if (!childVc) {
        
        childVc = [[ZStoreSubViewController alloc] init];
        if (self.dataArray.count>0) {
            childVc.datas = self.dataArray;
        }
        
    }
    return childVc;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}


#pragma mark - 轮播图视图

-(void)addBannerView{
    _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, cycleHight) delegate:self placeholderImage:nil];
    _cycleView.currentPageDotColor = kMagentaColor;
    _cycleView.pageDotColor = kLightGrayColor;
    _cycleView.autoScrollTimeInterval = 4;
    ///本地图片
//    _cycleView.localizationImageNamesGroup = @[];
    [self.view addSubview:self.cycleView];
}

- (void)loadBanner{
    
    NSDictionary *dic = @{@"type":@"4"};
    
    [ZNetworkService getRequestWithBaseUrl:kBannerUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        NSArray *array = result[@"result"];
        for (NSDictionary *dic in array) {
            BannerModel *model = [BannerModel mj_objectWithKeyValues:dic];
            NSString *url = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo];
            [self.bannerArray addObject:url];
        }
        
        self.cycleView.imageURLStringsGroup = self.bannerArray;
        
//        [self endRefresh];
    } fail:^(NSError *error) {
//        [self endRefresh];
    } delegater:nil];
}

- (void)loadStoreCategoryTitle{
    NSDictionary *dic = @{@"pageIndex":@"1",@"pageSize":@"10",};
    [ZNetworkService postRequestWithBaseUrl:kStoreBannerTitleUrl setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                
                StoreModel *model = [StoreModel mj_objectWithKeyValues:dic];
                 [self.dataArray addObject:model];
                NSString *title = model.className;
                [self.titleArray addObject:title];
            }
            [self addTabTitlesWithTitles:[self.titleArray copy]];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}


#pragma mark - sdcycle delegate

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
    
    
}


@end
